class Service:
    def __init__(self, n) -> None:
        self.number = n
        self.father = []
        self.son = None


class Solution:

    def __init__(self) -> None:
        self.n = int(input())
        self.edges = list(map(int, input().split()))
        self.services = [Service(i) for i in range(self.n)]
        for i in range(self.n):
            self.services[i].son = self.services[self.edges[i]]
            self.services[self.edges[i]].father.append(self.services[i])
        self.groups = []

    def dfs(self, service, group: list):
        group.append(service)

        next = service.son
        if next in group:  # 出现环形
            # 找到环形的初始服务（编号最小的服务）
            ptr = next.son
            start = next
            while ptr != next:
                if ptr.number < start.number:
                    start = ptr
                ptr = ptr.son
            # 查看该环是否已经记录
            recordFlag = False
            for g in self.groups:
                if g['Start'] == start:
                    recordFlag = True
                    break
            # 如果有记录，则更新该环的组信息
            if recordFlag:
                idx = group.index(next)
                g["V"] += idx
                g["H"] -= idx

            # 如果没有记录，则新增该环的记录
            else:
                idx = group.index(next)
                V = len(group)
                L = V - idx
                H = L - V
                Max = max([service.number for service in group[idx:]])
                self.groups.append({"L": L, "V": V,
                                    "H": H, "Max": Max,
                                    "Start": start})
        else:
            self.dfs(next, group)

    def parse(self):
        for service in self.services:
            if len(service.father) == 0:
                self.dfs(service, [])


run = Solution()
run.parse()


def key(group):
    return [-group['H'], group['Max']]


sorted(run.groups, key=key)
for group in run.groups:
    root = group["Start"]
    ptr = root
    while True:
        print(ptr.number, end=' ')
        ptr = ptr.son
        if ptr == root:
            break
    print('')
